package com.kun.reflection.demo01;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

//通过反射获得类的Class对象
public class Test02_getClass {

    @Test
    public void testGetClass() throws Exception {
        //1. 通过类名获得Class对象
        Class personClass_1 = Class.forName("com.kun.reflection.demo01.Person");
        System.out.println(personClass_1);

        //2. 测试多个CLass对象是否相同
        Class personClass_2 = Class.forName("com.kun.reflection.demo01.Person");
        System.out.println(personClass_1 == personClass_2); //true
        System.out.println(personClass_1.hashCode());   //445884362
        System.out.println(personClass_2.hashCode());   //445884362
    }

    @Test
    public void testGetClassByObject() throws Exception {
        //1. 包名+类名
        Class c1 = Class.forName("com.kun.reflection.demo01.Student");
        System.out.println(c1+"---"+c1.hashCode());

        //2. 类对象
        Student student = new Student();
        Class c2 = student.getClass();
        System.out.println(c2+"---"+c2.hashCode());

        //3. 类名.class
        Class c3 = Student.class;
        System.out.println(c3+"---"+c3.hashCode());

        //4. 内置对象的type属性
        Class<Integer> c4 = Integer.TYPE;
        System.out.println(c4+"---"+c4.hashCode());

        //5. 获得父类Class对象
        Class c5 = Student.class.getSuperclass();
        System.out.println(c5+"---"+c5.hashCode()); //class com.kun.reflection.demo01.Person---1031980531
    }


}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
    private String name;
}

@Data
class Student extends Person {
    public Student() {
        super("student");
    }
}

@Data
class Teacher extends Person {
    public Teacher() {
        super("teacher");
    }
}

